[SPCEL DIO INPUT/OUTPUT GUIDE (C# SAMPLE)]

1. REFERENCES
https://github.com/ellysh/InpOut32
https://www.highrez.co.uk/Downloads/InpOut32/default.htm

Please download "inpout32.dll" & "inpoutx64.dll" files from github url above.
Or you can download them directly from the link below:
https://www.highrez.co.uk/scripts/download.asp?package=InpOutBinaries


2. INITIALIZE DLL

        // Import inpout32.dll and define related functions
        [DllImport("inpout32.dll")]
        private static extern UInt32 IsInpOutDriverOpen();
        [DllImport("inpout32.dll")]
        private static extern void Out32(short PortAddress, short Data);
        [DllImport("inpout32.dll")]
        private static extern ushort Inp32(short PortAddress);

        // Import inpout64.dll and define related functions
        [DllImport("inpoutx64.dll", EntryPoint = "IsInpOutDriverOpen")]
        private static extern UInt32 IsInpOutDriverOpen_x64();
        [DllImport("inpoutx64.dll", EntryPoint = "Out32")]
        private static extern void Out32_x64(short PortAddress, short Data);
        [DllImport("inpoutx64.dll", EntryPoint = "Inp32")]
        private static extern ushort Inp32_x64(short PortAddress);
        
        // DIO port address 0x0AA8
        private const short DIO_PORT = 0x0AA8;

        private bool platrform64 = false;
        
        // Initialize inpout32.dll or inpout64.dll function
        public uint InitInpOut32orx64Dll()
        {
            Console.WriteLine("InitInpOut32orx64Dll");
            uint result = 0;
            try
            {
                try
                {
                    result = IsInpOutDriverOpen();
                    Console.WriteLine("32 result: {0}", result);
                }
                catch (BadImageFormatException)
                {
                    result = IsInpOutDriverOpen_x64();
                    platrform64 = result != 0;
                    Console.WriteLine("64 result: {0} platrform64: {1}", result, platrform64);
                }
            }
            catch (DllNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
		
3. READ ADN WRITE FUNCTION

        // Read function (DIO input values are bit 0 ~ 3)
        private short Read()
        {
            Console.WriteLine("Read");
            short readValue = ReadGPIOByte(DIO_PORT);
            Console.WriteLine("readValue: {0} hex: 0x{1}", readValue, readValue.ToString("X2"));
            return readValue;
        }

        // Write function (DIO output values are bit 4 ~ 7)
        private void Write(bool bit4, bool bit5, bool bit6, bool bit7)
        {
            Console.WriteLine("Write bits: {0} {1} {2} {3}", bit4, bit5, bit6, bit7);
            //BitArray writeValueBitArray = new BitArray(
            //    new bool[] { false, false, false, false, bit4, bit5, bit6, bit7 });
            Console.WriteLine("reverse bits: {0} {1} {2} {3}", !bit4, !bit5, !bit6, !bit7);
            BitArray writeValueBitArray = new BitArray(
                new bool[] { false, false, false, false, !bit4, !bit5, !bit6, !bit7 });
            int writeValue = 0;
            for (int i = 0; i < writeValueBitArray.Length; i++)
            {
                if (writeValueBitArray[i])
                    writeValue += Convert.ToInt16(Math.Pow(2, i));
            }
            Console.WriteLine("writeValue: {0} hex: 0x{1}", writeValue, writeValue.ToString("X2"));
            bool writeGPIOByteResult = WriteGPIOByte(DIO_PORT, (short)writeValue);
            Console.WriteLine("writeGPIOByteResult: {0}", writeGPIOByteResult);
            if (writeGPIOByteResult)
            {
                Thread.Sleep(1000);
                ReadButton_Click(this, null);
            }
        }

        // Read GPIO byte function
        private short ReadGPIOByte(short port)
        {
            Console.WriteLine("ReadGPIOByte port hex: 0x{0}", port.ToString(("X")));
            ushort result = 0;
            try
            {
                result = platrform64 ? Inp32_x64(port) : Inp32(port);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured:\n" + ex.Message);
            }
            return (short)(result & 0xFF);
        }

        // Write GPIO byte function
        private bool WriteGPIOByte(short port, short data)
        {
            Console.WriteLine("WriteGPIOByte port hex: 0x{0} data hex: 0x{1}", port.ToString("X4"), data.ToString("X2"));
            try
            {
                if (platrform64)
                {
                    Out32_x64(port, data);
                }
                else
                {
                    Out32(port, data);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occured:\n" + ex.Message);
                return false;
            }
            return true;
        }